package com.xinyue.game.message;

import com.xinyue.game.utils.ByteBufUtil;
import com.xinyue.game.utils.GameDateUtil;
import io.netty.buffer.ByteBuf;
import lombok.AccessLevel;
import lombok.Data;
import lombok.Setter;

import java.nio.charset.StandardCharsets;

/**
 * @author 王广帅
 * @since 2023/7/2 13:25
 **/
@Data
public class GameServerRequestHeader implements IGameMessageHeader {

    private String requestUri;

    @Setter(AccessLevel.NONE)
    private byte[] requestUriBytes;

    private String requestId;
    @Setter(AccessLevel.NONE)
    private byte[] requestIdBytes;

    // 客户端的ip地址
    private String clientIp;
    @Setter(AccessLevel.NONE)
    private byte[] clientIpBytes;
    // 用户标识，用于区分不同的请求者
    private String userKey;
    @Setter(AccessLevel.NONE)
    private byte[] userKeyBytes;

    private long requestTime;
    /**
     * 消息来自的网关节点id
     */
    private String clientInstanceId;
    @Setter(AccessLevel.NONE)
    private byte[] gatewayNodeIdBytes;

    @Override
    public void read(ByteBuf byteBuf) {
        this.requestUri = ByteBufUtil.readShortString(byteBuf);
        this.requestId = ByteBufUtil.readShortString(byteBuf);
        this.clientIp = ByteBufUtil.readShortString(byteBuf);
        this.userKey = ByteBufUtil.readShortString(byteBuf);
        this.requestTime = byteBuf.readLong();
        this.clientInstanceId = ByteBufUtil.readShortString(byteBuf);
    }

    @Override
    public int getHeaderTotalSize() {
        this.requestUriBytes = this.requestUri.getBytes(StandardCharsets.UTF_8);
        this.requestIdBytes = this.requestId.getBytes(StandardCharsets.UTF_8);
        this.clientIpBytes = clientIp.getBytes(StandardCharsets.UTF_8);
        this.userKeyBytes = this.userKey.getBytes(StandardCharsets.UTF_8);
        this.gatewayNodeIdBytes = this.clientInstanceId.getBytes(StandardCharsets.UTF_8);
        return 8 + 2 + this.requestUriBytes.length + 2 + this.requestIdBytes.length + 2 + this.clientIpBytes.length + 2 + this.userKeyBytes.length
                + 2 + this.gatewayNodeIdBytes.length;
    }

    @Override
    public void write(ByteBuf byteBuf) {
        byteBuf.writeShort(this.requestUriBytes.length);
        byteBuf.writeBytes(this.requestUriBytes);
        byteBuf.writeShort(this.requestIdBytes.length);
        byteBuf.writeBytes(this.requestIdBytes);

        byteBuf.writeShort(this.clientIpBytes.length);
        byteBuf.writeBytes(this.clientIpBytes);

        byteBuf.writeShort(this.userKeyBytes.length);
        byteBuf.writeBytes(this.userKeyBytes);

        byteBuf.writeLong(this.requestTime);

        byteBuf.writeShort(this.gatewayNodeIdBytes.length);
        byteBuf.writeBytes(this.gatewayNodeIdBytes);

    }

    @Override
    public String toString() {
        StringBuilder str = new StringBuilder();
        str.append("requestUri: ").append(this.requestUri)
                .append(", requestId: ").append(this.requestId)
                .append(", clientIP: ").append(this.clientIp)
                .append(", clientInstanceId: ").append(this.clientInstanceId)
                .append(", userKey: ").append(this.userKey)
                .append(", requestTime: ").append(GameDateUtil.getStandardDate(this.requestTime));
        return str.toString();
    }
}
